package com.apes.scm.account.account.model;

import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.util.ApesConst;
import com.apes.scm.account.tax.model.Tax;
import com.apes.scm.masterdata.frame.model.Dept;
import com.apes.scm.masterdata.party.model.PartyRole;
import com.apes.scm.masterdata.product.model.Product;
import com.apes.scm.masterdata.product.model.ProductUom;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.util.Date;

/**
 * 功能：客户寄售库存
 *
 * @author xul
 * @create 2018-03-14 13:35
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = InventoryConsign.CONST_TABLE_NAME,
        indexes = {
                @Index(name = "idx_consign_customer_id", columnList = "customer_id"),
                @Index(name = "idx_consign_company_id", columnList = "company_id"),
                @Index(name = "idx_consign_product_id", columnList = "product_id")
        })
public class InventoryConsign extends Model {

    public static final String CONST_TABLE_NAME = "acc_inventory_consign";

    @Id
    @Column(name = "id")
    @ApesGenerator(code = CONST_TABLE_NAME, name = "客户寄售库存")
    @Comment("批次内码")
    private long id;  //批次内码

    @Column(name = "batch_number", length = 32, nullable = false)
    @Comment("批次号")
    private long batchNumber;  //批次号

    @Column(name = "batch_serial", nullable = false)
    @Comment("批次序号")
    private Integer batchSerial = 0;  //批次序号

    @NotNull
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "company_id", referencedColumnName = "id")
    @Comment("公司编码")
    private PartyRole company;  //公司编码

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "dept_id", referencedColumnName = "id", nullable = false)
    @Comment("部门编码")
    private Dept dept;

    @NotNull
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "customer_id", referencedColumnName = "id")
    @Comment("客户")
    private PartyRole customer; //客户

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "product_id", referencedColumnName = "id", nullable = false)
    @Comment("商品编码")
    private Product product;  //商品编码

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "product_uom_id", referencedColumnName = "id", nullable = false)
    @Comment("计量单位")
    private ProductUom uom; //计量单位

    @NotNull
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "tax_id", referencedColumnName = "id")
    @Comment("税率")
    private Tax tax; //税率

    @Column(name = "price_unit", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("含税单价")
    private double priceUnit = 0.0; //含税单价

    @Column(name = "consign_qty", nullable = false, columnDefinition = ApesConst.FIELD_QTY_16_4_0)
    @Comment("寄售数量")
    private double consignQty = 0.0;  //寄售数量

    @Column(name = "return_qty", nullable = false, columnDefinition = ApesConst.FIELD_QTY_16_4_0)
    @Comment("寄售退回数量")
    private double returnQty = 0.0;  //寄售退回数量

    @Column(name = "price_change_qty", nullable = false, columnDefinition = ApesConst.FIELD_QTY_16_4_0)
    @Comment("变价数量")
    private double priceChangeQty = 0.0;   //变价数量

    @Column(name = "consume_qty", nullable = false, columnDefinition = ApesConst.FIELD_QTY_16_4_0)
    @Comment("消耗数量")
    private double consumeQty = 0.0;  //消耗数量

    @Column(name = "frost_consume_qty",  nullable = false, columnDefinition = ApesConst.FIELD_QTY_16_4_0)
    @Comment("冻结消耗数量")
    private double frostConsumeQty = 0.0;  //消耗结存数量

    @Column(name = "balance_qty", nullable = false, columnDefinition = ApesConst.FIELD_QTY_16_4_0)
    @Comment("结存数量")
    private double balanceQty = 0.0;  //结存数量

    @Column(name = "frost_balance_qty",  nullable = false, columnDefinition = ApesConst.FIELD_QTY_16_4_0)
    @Comment("冻结结存数量")
    private double frostBalanceQty = 0.0;  //冻结结存数量

    @Column(name = "In_date")
    @Comment("寄售日期")
    private Date InDate = new Date();  //寄售日期

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "inventory_batch_id", referencedColumnName = "id")
    @Comment("商品价值库存内码")
    private InventoryBatch inventoryBatch;

    /*
     * 功能：更新寄售结存数量
     */
    public void changeBalanceQty(double qty) {
        this.balanceQty += qty;
        if (this.balanceQty < 0) throw new RuntimeException("寄售批次内码["+ id + "]结存数量不允许小于零！");
        if (this.frostBalanceQty > this.balanceQty)  throw new RuntimeException("寄售批次内码["+ id + "]结存冻结数量不能大于结存数量！");
    }

    /*
     * 功能：更新寄售消耗数量
     */
    public void changeConsumeQty(double qty) {
        this.consumeQty += qty;
        if (this.consumeQty < 0) throw new RuntimeException("寄售批次内码["+ id + "]消耗数量不允许小于零！");
        if(this.frostConsumeQty > this.consumeQty) throw new RuntimeException("寄售批次内码["+ id + "]消耗冻结数量不允许大于消耗数量！");
    }

    /*
     * 功能：更新寄售退回数量
     */
    public void changeReturnQty(double qty) {
        this.returnQty += qty;
        if (this.returnQty < 0) throw new RuntimeException("寄售批次内码["+ id + "]退回数量不允许小于零！");
    }

    /*
     * 功能：更新消耗冻结数量
     */
    public void changeFrostConsumeQty(double qty) {
        this.frostConsumeQty += qty;
        if(this.frostConsumeQty < 0)  throw new RuntimeException("寄售批次内码["+ id + "]消耗冻结数量不允许小于零！");
        if(this.frostConsumeQty > this.consumeQty) throw new RuntimeException("寄售批次内码["+ id + "]消耗冻结数量不允许大于消耗数量！");
    }


    /*
     * 功能：更新结存冻结数量
     */
    public void changeFrostBalanceQty(double qty) {
        this.frostBalanceQty += qty;
        if(this.frostBalanceQty < 0)  throw new RuntimeException("寄售批次内码["+ id + "]结存冻结数量不允许小于零！");
        if(this.frostBalanceQty > this.balanceQty)  throw new RuntimeException("寄售批次内码["+ id + "]结存冻结数量不能大于结存数量！");
    }

    /*
     * 功能：更新变价数量
     */
    public void changePriceChangeQty(double qty) {
        this.priceChangeQty += qty;
        if(this.priceChangeQty < 0)  throw new RuntimeException("寄售批次内码["+ id + "]变价数量不允许小于零！");
    }


}
